/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.stats.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Marco Berri marcoberri@gmail.com
 */
public class All extends com.stats.Base {

    /**
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        super.processRequest(request,response);

        //statistiche di visualizzazione
        ArrayList list = new ArrayList();

        try {
            Statement stat = this.conn_stats.createStatement();
            Statement db = this.conn_url.createStatement();

            ResultSet rsstats = stat.executeQuery("select * from counter_view order by n_view desc limit 100");



            while (rsstats.next()) {
                HashMap<String, Object> m = new HashMap<String, Object>();
                ResultSet rs_db = db.executeQuery("select * from url where id = '" + rsstats.getString("fk_url_id") + "'");
                while (rs_db.next()) {
                    m.put("id", rs_db.getString("id"));
                    m.put("url", rs_db.getString("url"));
                    m.put("t", rs_db.getString("t"));
                    m.put("t_end", rs_db.getString("t_end"));
                }


                m.put("n_view", rsstats.getString("n_view"));
                list.add(m);
                rs_db.close();
            }

            rsstats.close();


        } catch (SQLException ex) {
            fatal("SQLException", ex);
        }


        response.setContentType("text/html;charset=" + this.conf.getUrl().getCharset());


        PrintWriter out = response.getWriter();
        HashMap<String, Object> data = new HashMap<String, Object>(2);
        data.put("stats", list);

        data.put("tot_view", tot_view);
        data.put("tot_save", tot_save);
        data.put("tot_ts", tot_ts);

        out.println(velocity.render("template/stats/all.vtl", data));
        out.close();


    }

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Statistiche di click sullo short url";
    }// </editor-fold>

    /**
     *
     * @param config
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

    }

    /**
     * Destroy
     */
    @Override
    public void destroy() {
        super.destroy();
    }
}
